package cn.est.controller;

import cn.est.dto.Result;
import cn.est.exceptions.RedisException;
import cn.est.exceptions.TokenProtectedException;
import cn.est.service.TokenService;
import cn.est.utils.ResultUtils;
import cn.est.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("token")
@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    TokenService tokenService;

    @ApiOperation(value = "token续签",produces="application/json",notes ="前端5分钟定时发送一次token续签请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value = "手机号码",required = true,dataType = "string"),
            @ApiImplicitParam(name="sms",value = "手机输入的验证码",required = true,dataType = "string")
    })
    @PostMapping(value = "/refresh",produces = "application/json")
    public Result refreshToken(@RequestHeader("token")String token ){
        TokenVo tokenVo=null;
        try {
            tokenVo=tokenService.refreshToken(token);
        } catch (RedisException e) {
            e.printStackTrace();
            return ResultUtils.returnFail(e.getMessage(),"20006");
        } catch (TokenProtectedException e) {
            e.printStackTrace();
            return ResultUtils.returnFail(e.getMessage(),"20006");
        }
        return  ResultUtils.returnDataSuccess(tokenVo);
    }
}
